package com.liugh.service.queue;


import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.entity.standard.StandardApprove;
import com.liugh.entity.standard.StandardDownload;
import com.liugh.enums.zhongche.ApproveEnum;
import com.liugh.enums.zhongche.DownloadEnum;
import com.liugh.service.IRedisService;
import com.liugh.service.zhongche.IStandardApproveService;
import com.liugh.service.zhongche.IStandardDownloadService;
import com.liugh.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.IOException;
import java.util.Date;

//redis消息队列，用于处理redis消息
public class RedisRecieve {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisRecieve.class);


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    IRedisService redisService;

    @Autowired
    IStandardApproveService approveService;

    @Autowired
    IStandardDownloadService downloadService;

    public RedisRecieve() {
    }

    /**
     * 处理上传审批
     * @param mes
     */
    public void ReviceMessage(String mes)  {
        mes = CommonUtil.jsonRidTrans(mes);
        LOGGER.info("收到的消息：" + mes);
        StandardApprove approve = new StandardApprove();
        try {
            approve = CommonUtil.fromJson(mes.substring(1, mes.length() - 1), StandardApprove.class);
            //处理碎片化
            approveService.handleApprove(approve);
        } catch (Exception e) {
            LOGGER.error("碎片化失败！：" + e.getMessage());
            if (approve != null) {
                approve.setAgreeTime(new Date());
                approve.setStatus(ApproveEnum.FUCK_FAIL.getCode());
                approve.setApproveUserNo(approveService.getUserByAppId(approve.getId()).getUserNo());
                approveService.updateApprove(approve);
                //同步更新item表
                approveService.synUpdateItem(approve.getId(),ApproveEnum.FUCK_FAIL.getCode());
            }
        }
    }

    /**
     * 处理下载审批
     *
     * @param mes
     */
    public void ReviceDownloadMessage(String mes) {
        mes = CommonUtil.jsonRidTrans(mes);
        LOGGER.info("申请下载收到的消息：" + mes);
        StandardDownload download = new StandardDownload();
        try {
            download = CommonUtil.fromJson(mes.substring(1, mes.length() - 1), StandardDownload.class);
            if (!downloadService.handleDownloadApply(download)) {
                if (download != null) {
                    download.setStatus(DownloadEnum.FAIL_WATER_MARK.getCode());
                    downloadService.updateDownload(download);
                }
                throw new BusinessException(PublicResultConstant.ERROR);
            }
        } catch (IOException e) {
            LOGGER.error("增加水印失败");
            e.printStackTrace();
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }

}
